package com.eascs.workflow.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.entity.sys.PlatformRegional;
import com.eascs.workflow.common.entity.sys.SysUser;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.sys.dao.SysOrgDao;
import com.eascs.workflow.sys.dao.SysUserDao;
import com.eascs.workflow.sys.service.OrgInletService;

@Service
public class OrgInletServiceImpl implements OrgInletService {
	@Autowired
	private SysOrgDao sysOrgDao;
	@Autowired
	private SysUserDao sysUserDao;

	@Override
	public List<PlatformRegional> getPlatformRegionalList() {
		List<PlatformRegional> resultList = new ArrayList<PlatformRegional>();
		// 先查出所有自然省
		List<ActOrgEntity> provinceList = sysOrgDao.getOrgProvince();
		for (ActOrgEntity provinceEntity : provinceList) {
			String regionalName = provinceEntity.getOrgName();
			// 查出大区
			ActOrgEntity virProvince = sysOrgDao.getOrgByIds(provinceEntity.getParentId());
			if (virProvince.getOrgLevel() == ActOrgEntity.ORG_LEVEL_PROVINCE
					&& virProvince.getIsNaturalProvince() != null && virProvince.getIsNaturalProvince() == 2) {
				regionalName = virProvince.getOrgName();
			}
			// 查询下面的所有平台
			List<ActOrgEntity> platformList = sysOrgDao.getLeafNodeByParentId(provinceEntity.getOrgId());
			for (ActOrgEntity platform : platformList) {
				PlatformRegional platformRegional = new PlatformRegional();
				platformRegional.setPlatFormId(platform.getUnitNo());
				platformRegional.setPlatFormCode(platform.getUnitCode());
				platformRegional.setPlatFormName(platform.getOrgName());
				platformRegional.setRegionalName(regionalName);
				platformRegional.setProvinceCode(provinceEntity.getUnitCode());
				platformRegional.setProvinceName(provinceEntity.getOrgName());
				platformRegional.setNatureProvince(platform.getProvince());
				platformRegional.setNatureCity(platform.getCity());
				platformRegional.setLevel(platform.getOrgLevel());
				resultList.add(platformRegional);
			}
		}
		return resultList;
	}

	@Override
	public ModelResult<List<ActOrgEntity>> getProvinceWithUser(String userNo) {
		ModelResult<List<ActOrgEntity>> modelResult = new ModelResult<List<ActOrgEntity>>();
		// 过滤重复的省区，正对 总部 + 省区 的人员
		Map<String, ActOrgEntity> provinceMap = new HashMap<String, ActOrgEntity>();
		List<ActOrgEntity> provinceResult = new ArrayList<ActOrgEntity>();
		// 用户所在树的节点
		List<ActOrgEntity> orgList = sysOrgDao.getOrgListByUserNo(userNo);
		if (CollectionUtils.isEmpty(orgList)) {
			modelResult.withModel(provinceResult);
			return modelResult;
		}

		for (ActOrgEntity org : orgList) {
			// 1.大区或者总部向下找省份
			if ((org.getOrgLevel() == ActOrgEntity.ORG_LEVEL_PROVINCE && org.getIsNaturalProvince() != null
					&& org.getIsNaturalProvince() == 2) || org.getParentId().equals("-1")) {
				List<ActOrgEntity> provinceList = sysOrgDao.getChildProvinceByNodeId(org.getOrgId());
				if (!CollectionUtils.isEmpty(provinceList)) {
					for (ActOrgEntity province : provinceList) {
						provinceMap.put(province.getOrgId(), province);
					}
				}
			} else if (org.getOrgLevel() == ActOrgEntity.ORG_LEVEL_PROVINCE
					&& (org.getIsNaturalProvince() == null || org.getIsNaturalProvince() == 1)) { // 2.省区不往下找
				provinceMap.put(org.getOrgId(), org);
			} else if (org.getOrgLevel() == ActOrgEntity.ORG_LEVEL_PLATFORM) { // 2.平台往上找省区
				ActOrgEntity province = sysOrgDao.getParentProviceByNodeId(org.getParentId());
				if (province != null) {
					provinceMap.put(province.getOrgId(), province);
				}
			}
		}

		provinceResult.addAll(provinceMap.values());
		modelResult.withModel(provinceResult);
		return modelResult;
	}

	@Override
	public ModelResult<List<SysUser>> getCreditMGRWithUser(String userNo) {
		ModelResult<List<SysUser>> modelResult = new ModelResult<List<SysUser>>();
		// 过滤重复的人员，正对 总部 + 省区 的人员
		Map<Integer, SysUser> creditMgrMap = new HashMap<Integer, SysUser>();
		List<SysUser> creditMgrList = new ArrayList<SysUser>();
		// 用户所在树的节点
		List<ActOrgEntity> orgList = sysOrgDao.getOrgListByUserNo(userNo);
		for (ActOrgEntity org : orgList) {
			// 1.总部查看所有
			if (org.getParentId().equals("-1")) {
				List<SysUser> userList = sysUserDao.getUserByOrgIdsAndRoleCode(null, "1007");
				for (SysUser user : userList) {
					creditMgrMap.put(user.getEmployeeNo(), user);
				}
			} else {
				List<String> orgIdList = new ArrayList<String>();
				// 如果是省平台要包含自己
				if (org.getOrgLevel() == ActOrgEntity.ORG_LEVEL_PLATFORM) {
					orgIdList.add(org.getOrgId());
				}
				// 所有的叶子节点
				List<ActOrgEntity> childOrgList = sysOrgDao.getLeafNodeByParentId(org.getOrgId());
				for (ActOrgEntity orgEntity : childOrgList) {
					orgIdList.add(orgEntity.getOrgId());
				}
				List<SysUser> userList = sysUserDao.getUserByOrgIdsAndRoleCode(orgIdList, "1007");
				for (SysUser user : userList) {
					creditMgrMap.put(user.getEmployeeNo(), user);
				}
			}
		}
		creditMgrList.addAll(creditMgrMap.values());
		modelResult.withModel(creditMgrList);
		return modelResult;
	}

	@Override
	public ModelResult<List<ActOrgEntity>> getPlatformWithUser(String userNo) {
		ModelResult<List<ActOrgEntity>> modelResult = new ModelResult<List<ActOrgEntity>>();
		// 过滤重复的省区，正对 总部 + 省区 的人员
		Map<String, ActOrgEntity> plateformMap = new HashMap<String, ActOrgEntity>();
		List<ActOrgEntity> platefromResult = new ArrayList<ActOrgEntity>();
		// 用户所在树的节点
		List<ActOrgEntity> orgList = sysOrgDao.getOrgListByUserNo(userNo);
		if (CollectionUtils.isEmpty(orgList)) {
			modelResult.withModel(platefromResult);
			return modelResult;
		}

		for (ActOrgEntity org : orgList) {
			// 1.总部返回所有平台
			if (org.getParentId().equals("-1")) {
				List<ActOrgEntity> platformList = sysOrgDao.getAllPlatformList();
				if (!CollectionUtils.isEmpty(platformList)) {
					for (ActOrgEntity platform : platformList) {
						plateformMap.put(platform.getOrgId(), platform);
					}
				}
			} else { // 2.找出所有的叶子节点
				// 如果是省平台要包含自己
				if (org.getOrgLevel() == ActOrgEntity.ORG_LEVEL_PLATFORM) {
					plateformMap.put(org.getOrgId(), org);
				}
				// 所有的叶子节点
				List<ActOrgEntity> childOrgList = sysOrgDao.getLeafNodeByParentId(org.getOrgId());
				for (ActOrgEntity orgEntity : childOrgList) {
					if (orgEntity.getOrgLevel() == ActOrgEntity.ORG_LEVEL_PLATFORM 
							|| orgEntity.getOrgLevel() == ActOrgEntity.ORG_LEVEL_PROVINCE_FIN_COM) {
						plateformMap.put(orgEntity.getOrgId(), orgEntity);
					}
				}
			}
		}
		platefromResult.addAll(plateformMap.values());
		modelResult.withModel(platefromResult);
		return modelResult;
	}

	@Override
	public ModelResult<List<SysUser>> getCreditMGRAndMarketerWithUser(String userNo) {
		List<String> roleCodeList = new ArrayList<String>();
		roleCodeList.add("1007");
		roleCodeList.add("1231");
		ModelResult<List<SysUser>> modelResult = new ModelResult<List<SysUser>>();
		// 过滤重复的人员，正对 总部 + 省区 的人员
		Map<Integer, SysUser> creditMgrMap = new HashMap<Integer, SysUser>();
		List<SysUser> creditMgrList = new ArrayList<SysUser>();
		// 用户所在树的节点
		List<ActOrgEntity> orgList = sysOrgDao.getOrgListByUserNo(userNo);
		for (ActOrgEntity org : orgList) {
			// 1.总部查看所有
			if (org.getParentId().equals("-1")) {
				List<SysUser> userList = sysUserDao.getUserByOrgIdsAndRoleCode(null, roleCodeList);
				for (SysUser user : userList) {
					creditMgrMap.put(user.getEmployeeNo(), user);
				}
			} else {
				List<String> orgIdList = new ArrayList<String>();
				// 如果是省平台要包含自己
				if (org.getOrgLevel() == ActOrgEntity.ORG_LEVEL_PLATFORM) {
					orgIdList.add(org.getOrgId());
				}
				// 所有的叶子节点
				List<ActOrgEntity> childOrgList = sysOrgDao.getLeafNodeByParentId(org.getOrgId());
				for (ActOrgEntity orgEntity : childOrgList) {
					orgIdList.add(orgEntity.getOrgId());
				}
				List<SysUser> userList = sysUserDao.getUserByOrgIdsAndRoleCode(orgIdList, roleCodeList);
				for (SysUser user : userList) {
					creditMgrMap.put(user.getEmployeeNo(), user);
				}
			}
		}
		creditMgrList.addAll(creditMgrMap.values());
		modelResult.withModel(creditMgrList);
		return modelResult;
	}
}
